<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_ticket") {

			  // Do some fetching
			  	$title				= $_POST['ticket_title'];
				$description		= $_POST['ticket_desc'];
				$keywords			= $_POST['ticket_keywords'];
				$component			= $_POST['ticket_component'];
				$ttype				= $_POST['ticket_type'];
				$componentversion	= $_POST['ticket_version'];
				$assign				= $_POST['ticket_assign'];
				$prio				= $_POST['ticket_prio'];
				$severity			= $_POST['ticket_severity'];
				$ticketmilestone	= $_POST['ticket_milestone'];
				$ticketstatus		= $_POST['ticket_status'];
				if ($user_ID != '') {
					$author			= $user_ID;
				} else {
					$nm_author			= $_POST['ticket_author'];
					$nm_authormail		= $_POST['author_email'];
					$author				= '2';
				}
				
			  // Add the content of the form to $post as an array
			  $new_ticket = array(
				  'post_title'    	=> $title,
				  'post_content'  	=> $description,
				  'post_category' 	=> array($component),
				  'tags_input'    	=> array($keywords),
				  'post_author' 	=> $author,
				  'post_status'   	=> 'publish',
			  );

			  // Save the new post
			  $pid = wp_insert_post($new_ticket, true);

			  // Set taxonomies
			  wp_set_post_terms($pid,array($componentversion),'component_version',true);
			  wp_set_post_terms($pid,array($ticketmilestone),'milestone',true);
			  wp_set_post_terms($pid,array($ttype),'typ',true);
			  wp_set_post_terms($pid,array($assign),'ticket_assign',true);
			  wp_set_post_terms($pid,array($prio),'prioritet',true);
			  wp_set_post_terms($pid,array($severity),'allvarlighetsgrad',true);
			  wp_set_post_terms($pid,array($ticketstatus),'ticket_status',true);
			  
			  // Set posters name if not logged in
			  if ($user_ID != '2') {
			  		add_post_meta($pid, 'ticket_poster', $nm_author);
					add_post_meta($pid, 'ticket_poster_mail', $nm_authormail);
			  }
			  
			  // Keep tags separated
			  wp_set_post_tags($pid, $keywords); 

			  // Redirect to the new post
			  $link = get_permalink( $pid );
				wp_redirect( $link );
				die();
			  }
			   
			  // Fetch some specific IDs for taxonomies
				  $assignID = get_term_by( 'name', 'Ej tilldelat', 'ticket_assign' );
				  $assignID = $assignID->term_id;
				  $prioID = get_term_by( 'name', 'Normal', 'prioritet' );
				  $prioID = $prioID->term_id;
				  $severityID = get_term_by( 'name', 'Normal', 'ticket_severity' );
				  $severityID = $severityID->term_id;
				  $statusID = get_term_by( 'name', 'Ny', 'ticket_status' );
				  $tstatusID = $statusID->term_id;
		  
get_header(); ?>
        
        <?php if (have_posts()) : ?>

		<?php while (have_posts()) : the_post(); ?>
        
            <h1 class="new-ticket-pagetitle"><?php the_title(); ?></h1>
            <section id="new-ticket">
                <form id="new_ticket" name="new_ticket" method="post" action="">
                    <p><label for="ticket_title">Rubrik</label><br /><input name="ticket_title" id="ticket_title" type="text" tabindex="1" class="title-text" /></p>                    
                    <p><label for="ticket_desc">Beskrivning</label></p>					
					<script type="text/javascript">edToolbar('ticket_desc');</script>
                    <p><textarea name="ticket_desc" cols="" rows="25" tabindex="2" class="desc-text" id="ticket_desc"></textarea></p>                    
                    <table class="select-table" cellspacing="0">
                    <tr>
                      <td>
                      <label for="ticket_component">Komponent</label><br />
                      <?php wp_dropdown_categories('tab_index=3&name=ticket_component&taxonomy=category&hide_empty=0&show_option_none=Ange...'); ?>
                      </td>
                      <td class="mid-col">
                      <label for="ticket_type">Typ</label><br />
                      <?php wp_dropdown_categories('tab_index=4&name=ticket_type&taxonomy=typ&hide_empty=0&show_option_none=Ange...'); ?>
                      </td>
                      <td>
                      <label for="ticket_version">Version</label><br />
                      <?php $cversions = get_terms('component_version','orderby=name&hide_empty=0') ?>
                      <select name="ticket_version" id="ticket_version" tabindex="5">
                        <option value="" selected="selected">Ange...</option>
                        <?php foreach ($cversions as $cversion) {
							echo '<option value="'.$cversion->name.'">'.$cversion->name.'</option>';
						} ?>
                      </select>
                      </td>
                    </tr>
                    <?php if ( current_user_can('editor') || current_user_can('administrator') ) { ?>
                    <tr>
                      <td>
                      <label for="ticket_assign">Tilldelat</label><br />
                      <?php wp_dropdown_categories('tab_index=6&name=ticket_assign&taxonomy=ticket_assign&hide_empty=0&selected='.$assignID); ?>
                      </td>
                      <td class="mid-col">
                      <label for="ticket_prio">Prioritet</label><br />
                      <?php wp_dropdown_categories('tab_index=7&name=ticket_prio&taxonomy=prioritet&hide_empty=0&selected='.$prioID); ?>
                      </td>
                      <td>
                      <label for="ticket_severity">Allvarlighetsgrad</label><br />
                      <?php wp_dropdown_categories('tab_index=8&name=ticket_severity&taxonomy=allvarlighetsgrad&hide_empty=0&selected='.$severityID); ?>
                      </td>
                    </tr>
                    <tr>
                      <td>
                      <label for="ticket_milestone">Målversion</label><br />
                      <?php $milestones = get_terms('milestone','orderby=name&hide_empty=0') ?>
                      <select name="ticket_milestone" id="ticket_milestone" tabindex="9">
                        <option value="Utvecklingsversion (trunk)">Ange...</option>
                        <?php foreach ($milestones as $milestone)
						{
							echo '<option value="'.$milestone->name.'">'.$milestone->name.'</option>';
						} ?>
                      </select>
                      </td>
                      <td class="mid-col">
                      <label for="ticket_status">Status</label><br />
                      <?php wp_dropdown_categories('tab_index=10&name=ticket_status&taxonomy=ticket_status&hide_empty=0&selected='.$tstatusID); ?>                      
                      </td>
                      <td>
                      <label for="ticket_keywords">Nyckelord</label><br />
                      <input name="ticket_keywords" id="ticket_keywords" placeholder="Separera med kommatecken" tabindex="11" />
                      </td>
                    </tr>
                    </table>
                    <?php } else { ?>
                    </table>
                      <div id="keywords-not-loggedin">
                        <label for="ticket_keywords">Nyckelord</label><br />
                        <input name="ticket_keywords" id="ticket_keywords" tabindex="11"  placeholder="Ange nyckelord (separera med kommatecken)" />
                      </div>
                      <input name="ticket_assign" id="ticket_assign" type="hidden" value="<?php echo $assignID; ?>" />
                      <input name="ticket_prio" id="ticket_prio" type="hidden" value="<?php echo $prioID; ?>" />
                      <input name="ticket_severity" id="ticket_severity" type="hidden" value="<?php echo $severityID; ?>" />
                      <input name="ticket_milestone" id="ticket_milestone" type="hidden" value="Utvecklingsversion (trunk)" />
                      <input name="ticket_status" id="ticket_status" type="hidden" value="<?php echo $tstatusID; ?>" />
                    <?php } ?>
                      <div id="new-ticket-note">
                        <h3>Innan du skickar din rapport</h3>
                        <ul>
                            <li>Kontrollera så att det inte redan har postats en rapport för samma ärende.</li>
                            <li>Se till att du har angett en kort men ändå förklarande rubrik för din rapport.</li>
                            <li>Se till att du beskriver din rapportering så detaljerat som möjligt.</li>
                        </ul>
                      </div>
                      <?php if ( is_user_logged_in() ) { ?>
                      <p class="logged-in-as">Du är inloggad som <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. Din rapport kommer att postas som denna användare. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Logga ut">Logga ut?</a></p>
                      <?php } else { ?>
                      <p><label for="ticket_author">Ditt namn</label><br /><input id="ticket_author" name="ticket_author" type="text" class="name-text" /></p>
                      <p><label for="author_email">E-post</label><br /><input id="author_email" name="author_email" type="text" class="mail-text" /></p>
                      <p><label>Antispam</label><br />
                      <span class="captcha"><?php mathCaptcha(); ?></span>
					  <input name="math" id="math" class="math-text" type="text" onkeypress="return isNumberKey(event);" /></p>
                      <?php } ?>
                  	  <div class="error_box" style="display:none;"></div>
                      <p><input id="submit" name="submit" type="submit" value="Skicka rapport" /></p>
                      <input type="hidden" name="action" value="new_ticket" />
                      <?php wp_nonce_field( 'new-ticket' ); ?>
                </form>
            </section>
        
		<?php endwhile; endif; ?>
            
<?php get_footer(); ?>